package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.controller.form.ChangeRoleNameForm;
import com.management.controller.form.RolePageForm;
import com.management.controller.info.RolePageInfo;
import com.management.model.Role;
import com.management.dao.RoleDao;
import com.management.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

    /**
     * 分页查询所有角色
     * @param rolePageForm 分页参数
     * @return
     */
    @Override
    public Result findRoleByPage(RolePageForm rolePageForm) {
        RolePageForm page = page(rolePageForm);
        List<RolePageInfo> list = CopyUtils.CopyList(page.getRecords(), RolePageInfo.class);
        Page<RolePageInfo> infoPage=new Page<>();
        infoPage.setRecords(list);
        infoPage.setCurrent(page.getCurrent());
        infoPage.setPages(page.getPages());
        infoPage.setTotal(page.getTotal());
        infoPage.setSize(page.getSize());
        return Result.getInstance(Page.class).setData(infoPage);
    }

    @Override
    public Result findAllRole() {
        List<Role> list = list();
        return Result.getInstanceList(Role.class).setData(list);
    }

    @Override
    public Result closeRole(Integer roleNum) {
        Role byId = getById(roleNum);
        if(byId.getRoleState().equals("有效")){
            byId.setRoleState("禁用");
            saveOrUpdate(byId);
            return Result.getInstance().setCode(200).setMessage("禁用成功");
        }else{
            byId.setRoleState("有效");
            saveOrUpdate(byId);
            return Result.getInstance().setCode(200).setMessage("启用成功");
        }

    }

    @Override
    public Result checkRoleState(Integer roleNum) {
        Role byId = getById(roleNum);
        if(byId.getRoleState().equals("有效")){
            return Result.getInstance().setCode(200);
        }
        return Result.getInstance().setCode(5001).setMessage("暂不可修改");
    }

    @Override
    public Result changeRoleName(ChangeRoleNameForm changeRoleNameForm) {
        if(changeRoleNameForm.getRoleName()==null||changeRoleNameForm.getRoleName().equals("")){
            return Result.getInstance().setCode(800).setMessage("请输入内容");
        }
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name",changeRoleNameForm.getRoleName());
        Role one = getOne(queryWrapper);
        if(one!=null){
            return Result.getInstance().setCode(5002).setMessage("该角色已存在");
        }
        Role byId = getById(changeRoleNameForm.getRoleNum());
        byId.setRoleName(changeRoleNameForm.getRoleName());
        saveOrUpdate(byId);
        return Result.getInstance().setCode(200).setMessage("修改成功");
    }

    @Override
    public Result delRole(Integer roleNum) {
        boolean b = removeById(roleNum);
        if(b){
            return Result.getInstance().setCode(200).setMessage("删除成功");
        }
        return Result.getInstance().setMessage("删除失败").setCode(5005);
    }
}
